<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猜数字游戏</title>
    <style>
        /* 基础样式重置与全局设置 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: "Microsoft YaHei", Arial, sans-serif;
        }

        body {
            background-color: #f0f5ff;
            color: #333;
            min-height: 100vh;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        /* 游戏容器 */
        .game-container {
            background-color: white;
            width: 100%;
            max-width: 600px;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        /* 标题样式 */
        .game-title {
            color: #2c6bed;
            font-size: 28px;
            margin-bottom: 15px;
        }

        /* 规则区域 */
        .game-rule {
            background-color: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 25px;
            text-align: left;
            font-size: 15px;
            line-height: 1.6;
        }

        .rule-title {
            font-weight: bold;
            color: #2c6bed;
            margin-bottom: 8px;
        }

        /* 输入区域 */
        .guess-area {
            margin-bottom: 25px;
        }

        .guess-input {
            width: 180px;
            padding: 12px 15px;
            border: 2px solid #ddd;
            border-radius: 6px;
            font-size: 16px;
            margin-right: 10px;
            outline: none;
            transition: border-color 0.3s;
        }

        .guess-input:focus {
            border-color: #2c6bed;
        }

        .guess-btn {
            padding: 12px 25px;
            background-color: #2c6bed;
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .guess-btn:hover {
            background-color: #1a56d9;
        }

        .reset-btn {
            padding: 10px 20px;
            background-color: #ff6b6b;
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 14px;
            cursor: pointer;
            margin-left: 10px;
            transition: background-color 0.3s;
        }

        .reset-btn:hover {
            background-color: #ff4757;
        }

        /* 结果反馈区域 */
        .result-area {
            height: 40px;
            margin-bottom: 20px;
            font-size: 18px;
            font-weight: bold;
        }

        .result-warm {
            color: #ff9800; /* 猜大/猜小提示色 */
        }

        .result-success {
            color: #4CAF50; /* 猜对提示色 */
        }

        .result-error {
            color: #ff4757; /* 输入错误提示色 */
        }

        /* 统计与历史记录 */
        .stats {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
            font-size: 15px;
        }

        .guess-count, .target-hint {
            color: #666;
        }

        .count-number, .hint-number {
            color: #2c6bed;
            font-weight: bold;
        }

        .history-area {
            text-align: left;
            margin-top: 25px;
        }

        .history-title {
            font-size: 16px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #2c6bed;
        }

        .history-list {
            list-style: none;
            background-color: #f8f9fa;
            border-radius: 8px;
            padding: 15px;
            max-height: 150px;
            overflow-y: auto;
        }

        .history-item {
            font-size: 14px;
            padding: 5px 0;
            border-bottom: 1px dashed #eee;
        }

        .history-item:last-child {
            border-bottom: none;
        }

        /* 响应式适配 */
        @media (max-width: 500px) {
            .guess-input {
                width: 100%;
                margin-right: 0;
                margin-bottom: 10px;
            }

            .guess-btn, .reset-btn {
                width: 100%;
                margin-left: 0;
                margin-bottom: 10px;
            }

            .stats {
                flex-direction: column;
                gap: 8px;
            }
        }
    </style>
</head>
<body>
    <div class="game-container">
        <!-- 游戏标题 -->
        <h1 class="game-title">猜数字游戏</h1>

        <!-- 游戏规则 -->
        <div class="game-rule">
            <div class="rule-title">游戏规则：</div>
            <ul>
                <li>1. 系统会随机生成一个 1-100 之间的整数作为目标数字</li>
                <li>2. 请输入你猜测的数字，点击「提交猜测」按钮</li>
                <li>3. 系统会提示「猜大了」或「猜小了」，帮助你缩小范围</li>
                <li>4. 猜对时会显示你的总猜测次数，点击「重新开始」可重置游戏</li>
            </ul>
        </div>

        <!-- 输入区域 -->
        <div class="guess-area">
            <input type="number" class="guess-input" placeholder="请输入1-100的数字" min="1" max="100">
            <button class="guess-btn">提交猜测</button>
            <button class="reset-btn">重新开始</button>
        </div>

        <!-- 结果反馈 -->
        <div class="result-area"></div>

        <!-- 统计信息 -->
        <div class="stats">
            <div class="guess-count">已猜次数：<span class="count-number">0</span></div>
            <div class="target-hint">目标范围：<span class="hint-number">1 - 100</span></div>
        </div>

        <!-- 历史记录 -->
        <div class="history-area">
            <div class="history-title">猜测历史：</div>
            <ul class="history-list"></ul>
        </div>
    </div>

    <script>
        // 游戏核心逻辑
        document.addEventListener('DOMContentLoaded', function() {
            // 获取DOM元素
            const guessInput = document.querySelector('.guess-input');
            const guessBtn = document.querySelector('.guess-btn');
            const resetBtn = document.querySelector('.reset-btn');
            const resultArea = document.querySelector('.result-area');
            const countNumber = document.querySelector('.count-number');
            const hintNumber = document.querySelector('.hint-number');
            const historyList = document.querySelector('.history-list');

            // 游戏初始数据
            let targetNumber; // 目标数字
            let guessCount = 0; // 猜测次数
            let minRange = 1; // 当前最小范围
            let maxRange = 100; // 当前最大范围
            let historyRecords = []; // 猜测历史

            // 初始化游戏（生成目标数字）
            function initGame() {
                targetNumber = Math.floor(Math.random() * 100) + 1;
                guessCount = 0;
                minRange = 1;
                maxRange = 100;
                historyRecords = [];
                
                // 更新页面显示
                countNumber.textContent = guessCount;
                hintNumber.textContent = `${minRange} - ${maxRange}`;
                resultArea.textContent = '';
                resultArea.className = 'result-area';
                guessInput.value = '';
                historyList.innerHTML = '';
                
                // 聚焦输入框
                guessInput.focus();
            }

            // 处理猜测逻辑
            function handleGuess() {
                // 获取输入值并验证
                const userGuess = parseInt(guessInput.value.trim());
                if (isNaN(userGuess) || userGuess < 1 || userGuess > 100) {
                    resultArea.textContent = '请输入1-100之间的有效数字！';
                    resultArea.className = 'result-area result-error';
                    guessInput.value = '';
                    guessInput.focus();
                    return;
                }

                // 检查是否重复猜测
                if (historyRecords.includes(userGuess)) {
                    resultArea.textContent = `你已经猜过${userGuess}了，换个数字试试！`;
                    resultArea.className = 'result-area result-warm';
                    guessInput.value = '';
                    guessInput.focus();
                    return;
                }

                // 更新猜测次数和历史
                guessCount++;
                historyRecords.push(userGuess);
                countNumber.textContent = guessCount;
                addHistoryRecord(userGuess);

                // 判断猜测结果
                if (userGuess > targetNumber) {
                    maxRange = userGuess; // 缩小最大范围
                    resultArea.textContent = `猜大了！当前范围：${minRange} - ${maxRange}`;
                    resultArea.className = 'result-area result-warm';
                } else if (userGuess < targetNumber) {
                    minRange = userGuess; // 扩大最小范围
                    resultArea.textContent = `猜小了！当前范围：${minRange} - ${maxRange}`;
                    resultArea.className = 'result-area result-warm';
                } else {
                    // 猜对了
                    resultArea.textContent = `恭喜你猜对了！目标数字是${targetNumber}，共猜了${guessCount}次！`;
                    resultArea.className = 'result-area result-success';
                    guessInput.disabled = true; // 禁用输入框
                    guessBtn.disabled = true; // 禁用提交按钮
                }

                // 更新范围提示和输入框
                hintNumber.textContent = `${minRange} - ${maxRange}`;
                guessInput.value = '';
                guessInput.focus();
            }

            // 添加历史记录到页面
            function addHistoryRecord(number) {
                const li = document.createElement('li');
                li.className = 'history-item';
                li.textContent = `第${guessCount}次：${number} → ${number > targetNumber ? '猜大了' : number < targetNumber ? '猜小了' : '猜对了！'}`;
                historyList.appendChild(li);
                // 滚动到最新记录
                historyList.scrollTop = historyList.scrollHeight;
            }

            // 绑定按钮事件
            guessBtn.addEventListener('click', handleGuess);
            resetBtn.addEventListener('click', function() {
                guessInput.disabled = false;
                guessBtn.disabled = false;
                initGame();
            });

            // 绑定回车键提交
            guessInput.addEventListener('keydown', function(e) {
                if (e.key === 'Enter') {
                    handleGuess();
                }
            });

            // 初始化游戏
            initGame();
        });
    </script>
    <a href="redir.html">重定向测试</a>

</body>
</html>